/* global routie */
/* global $ */
/* global App */
var socket = io.connect(); //与服务器进行连接  
var loginer = App.get("loginer");
routie.on("!/pages/people", function() {
	App.set("$Cache_new_msg", false);
});
$.ajax({
	url: "/api/complex/FriendsApi",
	type: "get",
	asycn: true,
	cache: false,
	data: {
		myid: loginer.sid
	},
	dataType: "json",
	success: function(result) {
		console.log(result);
		App.set("$Cache_friends_list", result);
	}
});
// 获取消息列表
App.set("$Event.change_chat", function() {
	var $Cache_chat_now = {};
	var $this = $(this);
	$this.removeClass("rec");
	$Cache_chat_now.logo = $this.find('img')[0].src;
	$Cache_chat_now.name = $this.find('.fname').text();
	$Cache_chat_now.chatid = $this.attr("id");
	$Cache_chat_now.rec_id = $this.attr("data");
	// console.log($this.attr("data"))
	App.set("$Cache_chat_now", $Cache_chat_now);
	console.log($Cache_chat_now);
	$.ajax({
		url: "/api/complex/ChatApi",
		type: "get",
		dataType: "json",
		asycn: false,
		cache: false,
		data: {
			chatid: $Cache_chat_now.chatid
		},
		success: function(result) {
			// console.log(JSON.stringify(result));
			App.set("$Cache_chat_now_list.chat", eval(result.chat));
			App.set("$Cache_now_id", $Cache_chat_now.chatid);
			var msg_scroll = document.getElementsByClassName("msgbox")[0];
			msg_scroll.scrollTop = msg_scroll.scrollHeight;
		}
	});
});

// 发送消息
App.set("$Event.send_chat_msg", function() {
	var msg = App.get("$Cache_send_chat_msg");
	App.set("$Cache_send_chat_msg", "");
	if (msg) {
		var new_msg = {};
		new_msg.time = (new Date).toLocaleString();
		new_msg.chat = msg;
		new_msg.chatid = loginer.sid;
		// new_msg.chattype = 1;
		App.push("$Cache_chat_now_list.chat", new_msg);
		App.set("$Cache_send_chat_msg", "");
		msg_dir(new_msg);
		new_msg.send_id = App.get("$Cache_chat_now.chatid");
		new_msg.rec_id = App.get("$Cache_chat_now.rec_id");
		socket.emit("sendfile", new_msg);
		var msg_scroll = document.getElementsByClassName("msgbox")[0];
		msg_scroll.scrollTop = msg_scroll.scrollHeight;
	} else {
		alert("请输入聊天内容！！！");
	};
});
// 发送图片
App.set("$Event.send_chat_img_click", function() {
	$("#send_chat_img").click();
})
App.set("$Event.send_chat_img", function() {
	var src = fileupload(this);
	// console.log(src);
	if (src) {
		var new_msg = {};
		new_msg.time = (new Date).toLocaleString();
		new_msg.src = src;
		new_msg.type = 2;
		new_msg.chattype = 2;
		App.push("$Cache_chat_now_list.chat", new_msg);
		msg_dir(new_msg);
		new_msg.send_id = App.get("$Cache_chat_now.chatid");
		new_msg.rec_id = App.get("$Cache_chat_now.rec_id");
		socket.emit("sendfile", new_msg);
	} else {
		alert("文件加载错误,请重试!!!");
	};
});

// 消息接收事件
socket.on("receive", function(data) {
	var msg_scroll = document.getElementsByClassName("msgbox")[0];
	// console.log(data);
	data.type = 1;
	if (data.rec_id == loginer.sid) {
		delete data.rec_id;
		if (data.send_id == App.get("$Cache_chat_now.chatid")) {
			delete data.send_id;
			App.push("$Cache_chat_now_list.chat", data);
			msg_scroll.scrollTop = msg_scroll.scrollHeight;
		} else {
			$("#" + data.send_id).addClass("rec");
		};
		// msg_dir(data);
	};
});
// 消息提交接口
function msg_dir(msg) {
	$.ajax({
		url: "api/complex/ChatApi",
		type: "put",
		data: {
			msg: msg,
			chatid: App.get("$Cache_chat_now.chatid")
		},
		success: function(result) {}
	});
}

App.set("$Event.show_clear_btn", function() {
	App.model.toggle("$Cache_clear_btn");
})